package com.learn.xueer.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import com.blankj.utilcode.util.ToastUtils
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseActivity
import com.learn.xueer.app.ext.init
import com.learn.xueer.app.ext.initMain
import com.learn.xueer.app.ext.interceptLongClick
import com.learn.xueer.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.mainBottom
import kotlinx.android.synthetic.main.activity_main.mainViewpager
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.network.manager.NetState

class MainActivity : BaseActivity<BaseViewModel, ActivityMainBinding>() {

    var exitTime = 0L

    override fun initView(savedInstanceState: Bundle?) {
        mainViewpager.initMain(this)
        //进入首页检查更新
//        Beta.checkUpgrade(false, true)
        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                //是主页
                if (System.currentTimeMillis() - exitTime > 2000) {
                    ToastUtils.showShort("再按一次退出程序")
                    exitTime = System.currentTimeMillis()
                } else {
                    finish()
                }
            }
        })


        mainBottom.init {
            when (it) {
                R.id.menu_home -> mainViewpager.setCurrentItem(0, false)
                R.id.menu_home_word -> mainViewpager.setCurrentItem(1, false)
                R.id.menu_mine -> mainViewpager.setCurrentItem(2, false)
            }
        }

        mainBottom.interceptLongClick(
            R.id.menu_home,
            R.id.menu_home_word,
            R.id.menu_mine
        )
        mainBottom.enableAnimation(false)
        mainBottom.enableShiftingMode(false)
        mainBottom.enableItemShiftingMode(false)
        mainBottom.itemIconTintList = null
    }

    /**
     * 示例，在Activity/Fragment中如果想监听网络变化，可重写onNetworkStateChanged该方法
     */
    override fun onNetworkStateChanged(netState: NetState) {
        super.onNetworkStateChanged(netState)
        if (netState.isSuccess) {
//            Toast.makeText(applicationContext, "我特么终于有网了啊!", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(applicationContext, "网络异常", Toast.LENGTH_SHORT).show()
        }
    }
}